package dfbz.线程练习;

public class Eat implements Runnable {
    //传入包子对象
    private Baozi baozi;

    public Eat(Baozi baozi) {
        this.baozi = baozi;
    }
    @Override
    public void run() {
        while (true){//一直吃包子
            //上锁
            synchronized (baozi){
                if(baozi.getFlag()%2==1){
                    System.out.println("正在吃"+baozi.getPrice()+"元的"+baozi.getName());
                    baozi.setFlag(baozi.getFlag()+1);//改变flag值
                    try {
                        baozi.notify();// //吃完唤醒制作包子的人 继续做包子
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
